#include <gtest/gtest.h>
#include "../mqserver/mq_consumer.hpp"

csjmq::ConsumerManager::ptr cmp;
class Consumertest : public testing::Environment
{
    virtual void SetUp() override
    {
        cmp = std::make_shared<csjmq::ConsumerManager>();
        cmp->initQueueConsumer("queue1");
    }

    virtual void TearDown() override
    {
        cmp->clear();
    }
};

void cb(const std::string &tag, const csjmq::BasicProperties *properties, const std::string &body)
{
    std::cout << tag << " 消费了消息:" << body << std::endl;
}
TEST(consumertest, create_test)
{
    cmp->create("consumer1", "queue1", false, cb);
    cmp->create("consumer2", "queue1", false, cb);
    cmp->create("consumer3", "queue1", false, cb);
    ASSERT_EQ(cmp->exists("consumer1", "queue1"), true);
    ASSERT_EQ(cmp->exists("consumer2", "queue1"), true);
    ASSERT_EQ(cmp->exists("consumer3", "queue1"), true);
}

TEST(consumertest, choose_test)
{
    csjmq::Consumer::ptr cp1 = cmp->choose("queue1");
    ASSERT_EQ(cp1->tag, "consumer1");
    auto cp2 = cmp->choose("queue1");
    ASSERT_EQ(cp2->tag, "consumer2");

    auto cp3 = cmp->choose("queue1");
    ASSERT_EQ(cp3->tag, "consumer3");

    auto cp4 = cmp->choose("queue1");
    ASSERT_EQ(cp4->tag, "consumer1");
}

TEST(consumertest, remove_test)
{
    cmp->remove("consumer1", "queue1");
    ASSERT_EQ(cmp->exists("consumer1", "queue1"), false);
}

int main(int argc, char *argv[])
{
    testing::AddGlobalTestEnvironment(new Consumertest);
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
